package com.treasuremall.auth.handler;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;
import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.authentication.www.BasicAuthenticationConverter;
import org.springframework.stereotype.Component;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 一句话描述功能
 *
 * @author 时前程
 * @create 2020-02-25 14:19
 * @Version V1.0
 **/
@Slf4j
@Component("appLoginInSuccessHandler")
@AllArgsConstructor
public class AppLoginInSuccessHandler implements AuthenticationSuccessHandler {

    private BasicAuthenticationConverter authenticationConverter;
    private ClientDetailsService clientDetailsService;
    private AuthorizationServerTokenServices authorizationServerTokenServices;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.info("登录成功处理");
        UsernamePasswordAuthenticationToken convert = authenticationConverter.convert(request);

        log.info("client-id:{}",convert.getPrincipal());
        log.info("client-secret:{}",convert.getCredentials());

        ClientDetails clientDetails = clientDetailsService.loadClientByClientId(convert.getPrincipal().toString());
        if (clientDetails == null) {
            throw new UnapprovedClientAuthenticationException("clientId对应的配置信息不存在"+convert.getPrincipal());
        }else if (!StringUtils.equals(convert.getCredentials().toString(),clientDetails.getClientSecret())){
            throw new UnapprovedClientAuthenticationException("clientSecret不匹配"+convert.getPrincipal());
        }

        TokenRequest tokenRequest = new TokenRequest(null,convert.getPrincipal().toString(),clientDetails.getScope(),"custom");
        OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
        OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request,authentication);
        OAuth2AccessToken accessToken = authorizationServerTokenServices.createAccessToken(oAuth2Authentication);

        response.setContentType("application/josn;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(accessToken));
    }
}
